function VehicleDetailView(currentWindow) {
	var self = Ti.UI.createView();

    var VehicleFillupsView = require('ui/VehicleFillupsView'),
    	VehicleStatsView = require('ui/VehicleStatsView'),
    	VehicleOtherView = require('ui/VehicleOtherView');
    	
    var fillUpsView = new VehicleFillupsView();
    var statsView = new VehicleStatsView();
    var otherView = new VehicleOtherView();
    
    // Add tabs to main view and only show the first
    self.add(fillUpsView);
    self.add(statsView);
    self.add(otherView);
    hideTheseTabs([statsView, otherView]);
    
	ModifyNavBar(self, currentWindow);
	
	// Create tabbed bar to match the respective views above
	var tabbedBar = Titanium.UI.iOS.createTabbedBar({
		labels:['Fill-ups', 'Statistics', 'Other'],
		backgroundColor:'maroon',
		index:0
	});
	
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	
	tabbedBar.addEventListener('click', function(e)
	{
		hideTheseTabs([fillUpsView, statsView, otherView]);
		if(tabbedBar.index == 0) {
			fillUpsView.show();	
		} else if (tabbedBar.index == 1) {
			statsView.show();
		} else if (tabbedBar.index == 2) {
			otherView.show();
		}
	});

	self.addEventListener('vehicleSelected', function(e) {
		
		// Set the current vehicle Id
		currentVehicleObj.vehicleId = e.vehicleId;
		currentVehicleObj.vehicleName = e.vehicleName;
		
		// Good idea:
		// In here, find out which view is active, and then send a fire event to
		// that specific view only.
		// TODO
		
		// We also want to make sure that only the first tab is shown and selected
		hideTheseTabs([statsView, otherView]);
		fillUpsView.show();
		tabbedBar.setIndex(0);
		
		// Make sure the fillups are updated
		fillUpsView.fireEvent('updateFillupsTableEvent', e);
		
		// Make sure the other tab knows the vehicleId at least
		otherView.fireEvent('updateVehicleOtherEvent', e);
		
		// Make sure the statistics tab is updated
		statsView.fireEvent('updateVehicleStatsEvent', e);
	});

	currentWindow.setToolbar([flexSpace,tabbedBar,flexSpace]);
	
	return self;
};

// This object contains only the vehicleId and the vehicleName, and is changed
// every time a new vehicle has been selected
var currentVehicleObj = {};

function ModifyNavBar(self, currentWindow) {

	var addNewEntryButton = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.ADD
	});
	
	addNewEntryButton.addEventListener('click', function(e)
	{
		self.fireEvent('addNewVehicleFillUpEvent', currentVehicleObj);
	});

	currentWindow.rightNavButton = addNewEntryButton;
}

function hideTheseTabs(otherTabs) {
	for(var i=0; i<otherTabs.length; i++) {
		otherTabs[i].hide();
   	}
}
module.exports = VehicleDetailView;
